#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        vector<vector<string>> result;
        int index = 0;  //标注是result中的第几个
        map<string,int> M;
        for(int i=0;i<strs.size();++i){
            string temp = strs[i];
            sort(temp.begin(),temp.end());
            if(M.find(temp)==M.end()){
                M.insert(make_pair(temp,index));
                vector<string> sonResult;
                sonResult.push_back(strs[i]);
                result.push_back(sonResult);
                index=index+1;
           }
           else{
               result[M[temp]].push_back(strs[i]);
           }

        }
        return result;   
    }
};

int main(){
    vector<string> v = {"eat", "tea", "tan", "ate", "nat", "bat"};
    Solution S;
    vector<vector<string>> result = S.groupAnagrams(v);
    system("pause");
}